package com.interfaces;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpSession;

import org.apache.log4j.Logger;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.common.web.CommonKeys;
import com.core.statistica.bo.StatisticaBo;
import com.core.statistica.dto.StatisticaDto;
import com.core.statistica.pojo.Statistica;
import com.core.user.pojo.User;
import com.core.visaInfo.dto.VisaInfoSearchCondition;
import com.util.DataBaseUtil;
import com.util.GsonUtil;
import com.util.framework.BaseController;

@Controller
public class StatisticaController extends BaseController{
	protected Logger log = Logger.getLogger(this.getClass());
	@Resource
	private StatisticaBo statisticaBo;
	
	public static final String URL_ENTITY = "/statistica";
	public static final String URL_RANGE_SEARCH = URL_ENTITY+"/RangeSearch";
	public static final String URL_SEARCH = URL_ENTITY+"/search";
	public static final String URL_GOTO_MAIN_PAGE = URL_ENTITY
			+ "/goto_main_page";
	
	@RequestMapping(value=URL_GOTO_MAIN_PAGE,method=RequestMethod.GET)
	public ModelAndView gotoMainPage (HttpSession session,VisaInfoSearchCondition sc){
		User userInfo =(User) session.getAttribute(CommonKeys.SESSION_USER);
		ModelAndView modelAndView =new ModelAndView();
		modelAndView.setViewName("/statistica/statistica_main");
		modelAndView.addObject(CommonKeys.SESSION_USER,userInfo);
		return modelAndView;
	}
	@RequestMapping(value = URL_SEARCH, method = RequestMethod.POST)
	@ResponseBody
	public String  getMonthData(String searchDay, HttpSession session) {
		SimpleDateFormat sdf1 = new SimpleDateFormat("yyyy-MM");
		Date searchDay1 = null;
		try {
			if (searchDay=="") {
				searchDay1 =new Date();
			}else{
				searchDay1 = sdf1.parse(searchDay);
			}
			
		} catch (ParseException e) {
			e.printStackTrace();
		}
		Map<String, Integer> finalResultMap =new LinkedHashMap<String, Integer>();
		StatisticaDto statisticaDto =new StatisticaDto();
		statisticaDto.setSearchDay(searchDay1);
		List<VisaInfoSearchCondition> visaList = statisticaBo.getvisaList(statisticaDto);
		List<Statistica> finalResultList =new ArrayList<Statistica>();
		for (VisaInfoSearchCondition visaInfoSearchCondition : visaList) {
				SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
				String keyString=sdf.format(visaInfoSearchCondition.getAddDate());
				Integer tempInteger =finalResultMap.get(keyString);
				if(tempInteger!=null){
					finalResultMap.put(keyString, tempInteger+1);
				}else{
					finalResultMap.put(keyString,1);
				}
		}
		for (Map.Entry<String,Integer> entry : finalResultMap.entrySet()){
			Statistica statistica =new Statistica();
			statistica.setDay(entry.getKey());
			statistica.setUsefulValue(entry.getValue().toString());
			finalResultList.add(statistica);
		}
		String resultString =GsonUtil.gson.toJson(finalResultList);
		return resultString;
	}
	@RequestMapping(value = URL_RANGE_SEARCH, method = RequestMethod.POST)
	@ResponseBody
	public String getRangeData(StatisticaDto statisticaDto, HttpSession session){
		DataBaseUtil<Statistica> db = new DataBaseUtil<Statistica>();
		String msg="";
		int totalCount = statisticaBo.getRangeDataCnt(statisticaDto);
		List<VisaInfoSearchCondition> SQLData = statisticaBo.getRangeData(statisticaDto);
		Map<String, Statistica> finalResultMap =new LinkedHashMap<String, Statistica>();
		List<Statistica>finalResultList = new ArrayList<Statistica>();
		for (VisaInfoSearchCondition visaInfoSearchCondition : SQLData) {
//			临时变量
			SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
			String keyString=sdf.format(visaInfoSearchCondition.getAddDate());
			Statistica tempStatistica =finalResultMap.get(keyString);
			
			if (tempStatistica==null) {//第一次进map的时候
				tempStatistica=new Statistica();
				if ("3".equals(visaInfoSearchCondition.getState())) {
					tempStatistica.setCanselCount(1);
				}else if("1".equals(visaInfoSearchCondition.getState())){
					tempStatistica.setSuccessCount(1);
				}else if ("2".equals(visaInfoSearchCondition.getState())||"0".equals(visaInfoSearchCondition.getState())) {
					tempStatistica.setFailCount(1);
				}
				tempStatistica.setVisaCount(1);
				finalResultMap.put(keyString,tempStatistica);
			}else{//第二次的时候按情况自加
				if (visaInfoSearchCondition.getState()=="3") {
					tempStatistica.setCanselCount(tempStatistica.getCanselCount()+1);
				}else if(visaInfoSearchCondition.getState()=="1"){
					tempStatistica.setSuccessCount(tempStatistica.getSuccessCount()+1);
				}else if (visaInfoSearchCondition.getState()=="2"||visaInfoSearchCondition.getState()=="0") {
					tempStatistica.setFailCount(tempStatistica.getFailCount()+1);
				}
				tempStatistica.setVisaCount(tempStatistica.getVisaCount()+1);
				finalResultMap.put(keyString,tempStatistica);
			}
		}
		for (Map.Entry<String,Statistica> entry : finalResultMap.entrySet()){
			Statistica statistica =entry.getValue();
			statistica.setDay(entry.getKey());
			finalResultList.add(statistica);
		}
		if (finalResultList.size()==0) {
			msg="{\"draw\":"+statisticaDto.getDraw()+",\"recordsTotal\":0,\"recordsFiltered\":0,\"data\": []}";
		}else{
			db.setTotalCount(totalCount);
			db.setRecordsFiltered(totalCount);
			db.setData(finalResultList);
			db.setDraw(statisticaDto.getDraw());
			msg = GsonUtil.gson.toJson(db);
		}
		return msg;
	}
}
